<%--
  Created by IntelliJ IDEA.
  User: 檬zhu
  Date: 2021/12/15
  Time: 23:57
  To change this template use File | Settings | File Templates.
--%>
<%@page import="java.util.List" %>
<%@ page import="com.example.ordersystem.domain.Order" %>
<%@ page import="com.example.ordersystem.domain.LineitemInfo" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/resources/layui/css/layui.css">
    <style type="text/css">
        form {
            margin-left: 5%;
            margin-top: 5%;
        }
    </style>
</head>
<body>
<form class="layui-form layui-form-pane" action="${pageContext.request.contextPath}/merchant/selectOrder"
      method="get">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">选择起点</label>
            <div class="layui-input-block" style="width: 190px">
                <input type="text" name="begin" id="date" autocomplete="off"
                       lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">选择终点</label>
            <div class="layui-input-block" style="width: 190px">
                <input type="text" name="end" id="date2" autocomplete="off"
                       lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="submit" class="layui-btn"
                    lay-submit="" lay-verify="select">查询
            </button>
        </div>
    </div>
</form>
<script src="${pageContext.request.contextPath}/resources/layui/layui.js"></script>
<script>
    layui.use(['form', 'laydate', 'table'], function () {
        var form = layui.form, laydate = layui.laydate;
        var table = layui.table;
        //日期
        laydate.render({
            elem: '#date'
            , type: 'datetime'
            , max: 0
        });
        //日期
        laydate.render({
            elem: '#date2'
            , type: 'datetime'
            , max: 0
        });
        //自定义验证规则
        form.verify({
            select: function (value) {
                var begin = document.getElementById('date').value;
                var end = document.getElementById('date2').value;
                if (begin > end) {
                    return '日期选择不合法，起点需要小于等于终点';
                }
            }
        });
    });
</script>
<%
    Object success = request.getAttribute("success");
    if (success != null) {
        out.println("<script language='JavaScript'>alert('退款成功');window.location.href='/merchant/allOrder'</script>");
    }

    Object obj = request.getAttribute("orders");
    if (obj == null) {
        return;
    } else {
        List<Order> orders = (List<Order>) obj;
%>
<table class="layui-table">
    <thead>
    <tr>
        <th>编号</th>
        <th>顾客</th>
        <th>小计</th>
        <th>提交时间</th>
        <th>收货地址</th>
        <th>留言</th>
        <th>订单详情</th>
        <th>状态</th>
    </tr>
    </thead>
    <tbody>
    <%
        double sum = 0;
        for (Order order : orders) {
            sum += order.getTotal();
    %>
    <tr>
        <td><%=order.getNum()%>
        </td>
        <td><%=order.getCustomer()%>
        </td>
        <td><%=order.getTotal()%>
        </td>
        <td><%=order.getBirth()%>
        </td>
        <td><%=order.getCustomeraddress()%>
        </td>
        <td><%=order.getRemark()%>
        </td>
        <td>
            <a href="${pageContext.request.contextPath}/customer/orderDetail?orderNum=<%=order.getNum()%>" class="layui-btn" style="background-color:#ff896b;">详情</a>
        </td>
        <%
            /*支付成功状态*/
            if (order.getStatus().equals(Order.TRADE_STATUS.get(Order.SUCCESS))) {
        %>
        <td>
            <input type="button" value="<%=order.getStatus()%>" class="layui-btn layui-btn-warm" style="width: 90px">
        </td>
        <%
            }
        %>
        <%
            /*交易完成*/
            if (order.getStatus().equals(Order.TRADE_STATUS.get(Order.FINISHED))) {
        %>
        <td>
            <input type="button" value="<%=order.getStatus()%>" class="layui-btn" style="width: 90px">
        </td>
        <%
            }
        %>

        <%
            /*待付款状态*/
            if (order.getStatus().equals(Order.TRADE_STATUS.get(Order.WAIT_BUYER_PAY))) {
        %>
        <td>
            <input type="button" value="<%=order.getStatus()%>" class="layui-btn layui-btn-normal" style="width: 90px">
        </td>
        <%
            }
        %>

        <%
            /*退款中状态*/
            if (order.getStatus().equals(Order.REFUNDING)) {
        %>
        <td>
            <form action="${pageContext.request.contextPath}/merchant/refund" style="margin-left: 0">
                <input type="submit" value="同意退款" class="layui-btn layui-btn-danger" style="width: 90px">
                <input type="hidden" name="orderNum" value="<%=order.getNum()%>">
                <input type="hidden" name="amount" value="<%=order.getTotal()%>">
            </form>
        </td>
        <%
            }
        %>

        <%
            /*交易关闭状态*/
            if (order.getStatus().equals(Order.TRADE_STATUS.get(Order.CLOSED))) {
        %>
        <td>
            <input type="button" value="<%=order.getStatus()%>" class="layui-btn layui-btn-primary" style="width: 90px">
        </td>
        <%
            }
        %>

    </tr>

    <%
        }
    %>
    </tbody>
</table>

<button type="button" class="layui-btn layui-btn-radius">共<%=orders.size()%>单</button>
<button type="button" class="layui-btn layui-btn-warm layui-btn-radius">总计<%=sum%>元</button>
<%
    }
%>

</body>
</html>